package com.yjzx.interfaces;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

/**
 * @author yjzx
 * @date 2024/4/21
 * @description
 */
public interface CommonClasses {

    static Class<?> getClass(ClassPool classPool, String className) {
        Class<?> clazz;
        try {
            CtClass ctClass = getCtClass(classPool, className);

            try {
                clazz = classPool.toClass(ctClass);
            } catch (SecurityException e) {
                clazz = Class.forName(ctClass.getName());
            } catch (CannotCompileException e) {
                clazz = baseClass(className);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return clazz;
    }

    static Class<?> baseClass(String className) throws ClassNotFoundException {
        switch (className) {
            case "boolean":
                return boolean.class;
            case "byte":
                return byte.class;
            case "short":
                return short.class;
            case "int":
                return int.class;
            case "long":
                return long.class;
            case "float":
                return float.class;
            case "double":
                return double.class;
            case "char":
                return char.class;
            case "void":
                return void.class;
            default:
                return Class.forName(className);
        }
    }

    static CtClass getCtClass(ClassPool classPool, String className) {
        try {
            String fullClassName = CommonClasses.classNameConvert(className);
            switch (fullClassName) {
                case "boolean":
                    return CtClass.booleanType;
                case "byte":
                    return CtClass.byteType;
                case "short":
                    return CtClass.shortType;
                case "int":
                    return CtClass.intType;
                case "long":
                    return CtClass.longType;
                case "float":
                    return CtClass.floatType;
                case "double":
                    return CtClass.doubleType;
                case "char":
                    return CtClass.charType;
                case "void":
                    return CtClass.voidType;
                default:
                    return classPool.get(CommonClasses.classNameConvert(fullClassName));
            }
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 类名转换
     *
     * @param className
     * @return
     */
    static String classNameConvert(String className) {
        switch (className) {
            case "String":
                return "java.lang.String";
            case "Byte":
                return "java.lang.Byte";
            case "Short":
                return "java.lang.Short";
            case "Character":
                return "java.lang.Character";
            case "Integer":
                return "java.lang.Integer";
            case "Long":
                return "java.lang.Long";
            case "Boolean":
                return "java.lang.Boolean";
            case "Double":
                return "java.lang.Double";
            case "Float":
                return "java.lang.Float";
            case "Object":
                return "java.lang.Object";
            case "override":
                return "java.lang.Override";
            case "Serializable":
                return "java.io.Serializable";
            case "Function":
                return "java.util.function.Function";
            case "Supplier":
                return "java.util.function.Supplier";
            case "Consumer":
                return "java.util.function.Consumer";
            case "Predicate":
                return "java.util.function.Predicate";
            default:
                return className;
        }
    }
}
